import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:smart_devices/icons/flutter_icons.dart';
import 'package:smart_devices/model/message.dart';
import 'package:smart_devices/utils/colors.dart';
import 'package:smart_devices/utils/styles.dart';
import 'package:smart_devices/views/device/repair/info/info_view.dart';

import 'list_logic.dart';

class MessageListView extends GetView<MessageListLogic> {
  const MessageListView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor:
          ColorsUtils.parseColorFromHexString(ColorsUtils.colorF6f6),
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(
            Icons.arrow_back_ios,
            color: Colors.white,
            size: 15,
          ),
          onPressed: Get.back,
        ),
        automaticallyImplyLeading: false,
        backgroundColor:
            ColorsUtils.parseColorFromHexString(ColorsUtils.color0052),
        title: Text(
          '系统消息',
          style: StylesUtils.customTextStyle(
              fontSize: 18, fontWeight: FontWeight.w700),
        ),
        centerTitle: true,
        actions: [
          InkWell(
              onTap: () {},
              child: Container(
                alignment: Alignment.center,
                padding: const EdgeInsets.only(right: 10),
                child: Text(
                  '清除未读',
                  style: StylesUtils.customTextStyle(fontSize: 13),
                ),
              ))
        ],
        elevation: 0,
      ),
      body: Obx(() => createdBody(context)),
    );
  }

  Widget createdBody(BuildContext context) {
    return CustomScrollView(
      controller: controller.scroll,
      slivers: [
        SliverToBoxAdapter(
          child: Container(
            height: 10,
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF6f6),
          ),
        ),
        SliverPersistentHeader(
          pinned: true,
          delegate: SliverHeaderDelegate(
              maxHeight: 50,
              minHeight: 50,
              child: Container(
                  height: 50,
                  decoration: const BoxDecoration(color: Colors.white),
                  padding: EdgeInsets.symmetric(
                      horizontal: (Get.width - 112 * 3) / 2),
                  alignment: Alignment.center,
                  child: Obx(() => CupertinoSegmentedControl<int>(
                        onValueChanged: (int value) {
                          controller.tabIndex.value = value;
                          controller.scroll.animateTo(0,
                              duration: const Duration(milliseconds: 500),
                              curve: Curves.linear);
                        },
                        children: {
                          0: Container(
                            height: 30,
                            width: 112,
                            alignment: Alignment.center,
                            decoration: const BoxDecoration(
                                borderRadius: BorderRadius.only(
                                    topLeft: Radius.circular(5),
                                    bottomLeft: Radius.circular(5))),
                            child: Text('全部',
                                style: StylesUtils.customTextStyle(
                                    color: controller.tabIndex.value == 0
                                        ? ColorsUtils.colorFfff
                                        : ColorsUtils.color0052)),
                          ),
                          1: Container(
                            height: 30,
                            width: 112,
                            alignment: Alignment.center,
                            child: Text('已读',
                                style: StylesUtils.customTextStyle(
                                    color: controller.tabIndex.value == 1
                                        ? ColorsUtils.colorFfff
                                        : ColorsUtils.color0052)),
                          ),
                          2: Container(
                            height: 30,
                            width: 112,
                            alignment: Alignment.center,
                            decoration: const BoxDecoration(
                                borderRadius: BorderRadius.only(
                                    topRight: Radius.circular(5),
                                    bottomRight: Radius.circular(5))),
                            child: Text('未读',
                                style: StylesUtils.customTextStyle(
                                    color: controller.tabIndex.value == 2
                                        ? ColorsUtils.colorFfff
                                        : ColorsUtils.color0052)),
                          ),
                        },
                        groupValue: controller.tabIndex.value,
                        borderColor: ColorsUtils.parseColorFromHexString(
                            ColorsUtils.color0052),
                        unselectedColor: Colors.transparent,
                        selectedColor: ColorsUtils.parseColorFromHexString(
                            ColorsUtils.color0052),
                      )))),
        ),
        SliverToBoxAdapter(
          child: Container(
            height: 10,
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF6f6),
          ),
        ),
        ...headerToBox()
      ],
    );
  }

  List<Widget> headerToBox() {
    return controller.pageList
        .map((e) => SliverToBoxAdapter(
              child: Container(
                margin: const EdgeInsets.only(left: 10, right: 10, top: 10),
                padding: const EdgeInsets.symmetric(vertical: 10),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(5)),
                child: ListBody(
                  children: [
                    Row(
                      children: [
                        Container(
                          width: 30,
                          height: 40,
                          alignment: Alignment.center,
                          child: Text(
                            '●',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.colorEb65, fontSize: 10),
                          ),
                        ),
                        Expanded(
                            child: Container(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            e.addTime,
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color9999),
                          ),
                        )),
                        InkWell(
                          onTap: () {},
                          child: Container(
                            width: 40,
                            height: 40,
                            alignment: Alignment.center,
                            child: Icon(
                              AntDesign.right,
                              color: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color9999),
                              size: 15,
                            ),
                          ),
                        )
                      ],
                    ),
                    Container(
                      height: 50,
                      alignment: Alignment.centerLeft,
                      padding: const EdgeInsets.only(left: 15),
                      child: Text(
                        e.title,
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color3333),
                      ),
                    ),
                    Row(
                      children: [
                        Container(
                          width: 80,
                          height: 40,
                          alignment: Alignment.center,
                          child: Text(
                            '任务类型',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color9999),
                          ),
                        ),
                        Expanded(
                            child: Container(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            e.type.label,
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color3333),
                          ),
                        ))
                      ],
                    ),
                    if (e.applicant != null)
                      Row(
                        children: [
                          Container(
                            width: 80,
                            height: 40,
                            alignment: Alignment.center,
                            child: Text(
                              '申请人',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color9999),
                            ),
                          ),
                          Expanded(
                              child: Container(
                            alignment: Alignment.centerLeft,
                            child: Text(
                              e.applicant ?? '',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color3333),
                            ),
                          ))
                        ],
                      ),
                    Row(
                      children: [
                        Container(
                          width: 80,
                          height: 40,
                          alignment: Alignment.center,
                          child: Text(
                            getLabelByType(e.type),
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color9999),
                          ),
                        ),
                        Expanded(
                            child: Container(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            e.rangeTime,
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color3333),
                          ),
                        ))
                      ],
                    )
                  ],
                ),
              ),
            ))
        .toList();
  }

  String getLabelByType(MessageTypeEnum type) {
    switch (type) {
      case MessageTypeEnum.upkeepTask:
        return '任务时间';
      case MessageTypeEnum.faultAudit:
        return '报修时间';
      case MessageTypeEnum.spareReceiveAudit:
        return '申请时间';
      case MessageTypeEnum.repairOrder:
        return '工单时间';
      default:
        return '任务时间';
    }
  }
}
